package cn.blnp.net.flowable.consumer;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * <h3></h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/5/14 11:41
 */
@Slf4j
@Component
public class ResultProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {
        //获取Camel调用获取结果
        String callResult = exchange.getIn().getBody(String.class);
        //打印Camel调用结果
        log.info("Camel调用结果为：{}", callResult);
        //转换成Map
        Map<String, String> callResultMap = JSON.parseObject(callResult, Map.class);
        //过滤得到需要的键值对
        Map<String, String> resultMap = callResultMap.entrySet().stream()
                .filter(map -> "ip".equals(map.getKey()) || "pro".equals(map.getKey())
                        || "city".equals(map.getKey()) || "addr".equals(map.getKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        //将Camel消息体作为Map回传
        exchange.getMessage().setBody(resultMap, Map.class);
    }
}
